/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_framework/zhiqim_kernel.htm
 *
 * Zhiqim Kernel is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.kernel.schedule;

import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.util.DateTimes;

/**
 * 每小时任务，设置分、秒后在该点运行
 * 时间格式：yyyyMMddHH
 *
 * @version v1.0.0 @author zouzhigang 2014-2-27 新建与整理
 */
@AnAlias("Hour")
public final class Hour extends TaskThreader
{
    private int taskMinute  = 0;            //指定处理的分钟
    private int taskSecond  = 0;            //指定处理的秒
    
    /** 空构造函数,用于newInstance后再设置参数 */
    public Hour(){}
    
    /**
     * 设置定时安排时间点，指定每日的时该任务运行
     * 
     * @param task      任务
     * @param minute    指定分钟
     * @param second    指定秒
     */
    public Hour(Task task, int minute, int second)
    {
        schedule(task, new int[]{minute, second});
    }
    
    @Override /** 任务名称 */
    public String getName()
    {
        return "Hour";
    }
    
    @Override /** 任务时间 */
    public String getTime()
    {
        return new StringBuilder().append(taskMinute).append("/").append(taskSecond).toString();
    }
    
    @Override /** 时钟安排 */
    protected TaskThreader schedule(Task task, int[] time)
    {
        if (task == null || time == null || time.length != 2)
            throw new RuntimeException("Hour传入的参数为空或数组长度不对");
        
        if (time[0] < 0 || time[0] > 59 || time[1] < 0 || time[1] > 59)
            throw new RuntimeException("Hour传时的时间格式不正确");
        
        super.task = task;
        this.taskMinute = time[0];
        this.taskSecond = time[1];
        return this;
    }
    
    @Override /** 计算上次执行时刻 */
    protected long calcLastTime(int curYearMonth, int curMonthMaxDay, int curDay, int curHour, int curMinute, int curSecond, int curWeek)
    {
        return curYearMonth * 10000L + curDay * 100 + curHour;
    }
    
    @Override /** 计算上次执行时刻的下一个时刻 */
    protected long calcLastTimeNextTime()
    {
        return DateTimes.getNextHour(lastTime);
    }
    
    @Override /** 验证时间是否到达 */
    protected final boolean isArrive(int curYearMonth, int curMonthMaxDay, int curDay, int curHour, int curMinute, int curSecond, int curWeek)
    {
        //当前小时，分钟未达
        if (curMinute < taskMinute)
            return false;
            
        //当前小时，分钟已达
        if (curMinute > taskMinute)
            return true;
        
        //当前小时，分钟相等，比较秒数
        return curSecond >= taskSecond;
    }
    
    public int getTaskMinute()
    {
        return taskMinute;
    }

    public int getTaskSecond()
    {
        return taskSecond;
    }
}
